package com.lyy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyy.entity.Video;
import com.lyy.mapper.VideoMapper;
import com.lyy.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Liangyaoyun
 */
@Service
@CacheConfig(cacheNames = "lesson:cache:video")
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;

    @Cacheable(key = "#videoId", condition = "#videoId != null")
    @Override
    public Video selectByVideoId(Integer videoId) {

        // DB: 按视频主键查询一条视频记录
        Video condition = new Video();
        condition.setVideoId(videoId);
        List<Video> videos = videoMapper.select(condition);
        if (ObjectUtil.isEmpty(videos)) {
            throw new RuntimeException("视频查询失败: 视频表主键不存在");
        }

        return videos.get(0);
    }

    @Override
    public PageInfo<Video> searchByVideoTitle(String videoTitle,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Video> condition=videoMapper.selectLikeVideoTitle(videoTitle);
        if (ObjectUtil.isEmpty(videoTitle)||videoTitle.equals("")){
            throw new RuntimeException("用户查询失败: 你所搜的视频为空!");
        }

        return new  PageInfo<>(condition);
    }
}
